import logging
from collections import OrderedDict
from datetime import datetime

from rest_framework import filters, status
from rest_framework import viewsets

from utils.pagination import MyPageNumberPagination
from .models import Daily
from . import serializers
from rest_framework.decorators import action
from rest_framework.response import Response
from django_filters import rest_framework as django_filters

logger = logging.getLogger('dev07')


class DailyFilter(django_filters.FilterSet):
    name = django_filters.CharFilter(field_name="name", lookup_expr="icontains")
    demand_id = django_filters.NumberFilter(field_name="demand_id", lookup_expr="exact")
    version = django_filters.CharFilter(field_name="version", lookup_expr="icontains")
    tester = django_filters.CharFilter(field_name="tester", lookup_expr="icontains")
    status = django_filters.CharFilter(field_name="status", lookup_expr="icontains")
    test_date_start = django_filters.DateFilter(field_name="test_date", lookup_expr="gte")
    test_date_end = django_filters.DateFilter(field_name="test_date", lookup_expr="lte")
    launch_time_start = django_filters.DateFilter(field_name="launch_time", lookup_expr="gte")
    launch_time_end = django_filters.DateFilter(field_name="launch_time", lookup_expr="lte")

    class Meta:
        model = Daily
        fields = ["demand_id", "name", "version", "tester", "test_date", "launch_time","status"]


class DailyViewSet(viewsets.ModelViewSet):
    """
    list:
    获取日报

    retrieve:
    获取日报详情数据

    update:
    更新日报信息

    """
    queryset = Daily.objects.all()
    serializer_class = serializers.DailyModelSerailizer

    # filter_backends = [filters.SearchFilter, filters.OrderingFilter]
    filter_backends = [django_filters.DjangoFilterBackend, filters.OrderingFilter]
    filterset_class = DailyFilter
    # search_fields = ['=name', '=leader', '=id']
    ordering_fields = ['launch_time']
    pagination_class = MyPageNumberPagination
    # permission_classes = [permissions.IsAuthenticated]

    # 自定义动作，通过POST请求接收JSON参数

    # def update(self, request, *args, **kwargs):
    #     partial = kwargs.pop('partial', False)
    #     instance = Daily.objects.filter(pk=request.data['pk'])
    #     serializer = self.get_serializer(instance, data=request.data, partial=partial)
    #     serializer.is_valid(raise_exception=True)
    #     self.perform_update(serializer)
    #
    #     if getattr(instance, '_prefetched_objects_cache', None):
    #         # If 'prefetch_related' has been applied to a queryset, we need to
    #         # forcibly invalidate the prefetch cache on the instance.
    #         instance._prefetched_objects_cache = {}
    #
    #     return Response(serializer.data)
    # def filter_queryset(self, queryset):
    #     if self.action == 'partial_update':
    #         id_ = self.kwargs.get('pk')
    #         queryset = queryset.filter(id=int(id_))
    #         return queryset
    #     else:
    #         queryset = super().filter_queryset(queryset)
    #         return queryset



